using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;
using System.Security.Cryptography;

namespace PT.Common
{
    public static class Utils
    {

        /// <summary>
        /// MD5函数
        /// </summary>
        /// <param name="str">原始字符串</param>
        /// <returns>MD5结果</returns>
        public static string MD5(string str)
        {
            byte[] b = Encoding.Default.GetBytes(str);
            b = new MD5CryptoServiceProvider().ComputeHash(b);
            string ret = "";
            for (int i = 0; i < b.Length; i++)
                ret += b[i].ToString("x").PadLeft(2, '0');
            return ret;
        }

        /// <summary>
        /// SHA256函数
        /// </summary>
        /// /// <param name="str">原始字符串</param>
        /// <returns>SHA256结果</returns>
        public static string SHA256(string str)
        {
            byte[] SHA256Data = Encoding.UTF8.GetBytes(str);
            SHA256Managed Sha256 = new SHA256Managed();
            byte[] Result = Sha256.ComputeHash(SHA256Data);
            return Convert.ToBase64String(Result);  //返回长度为44字节的字符串
        }

        /// <summary>
        /// 号码脱敏，将中间4位数字替换为*
        /// </summary>
        /// <param name="mobileNum"></param>
        /// <returns></returns>
        public static string mobile_transfer(string mobileNum)
        {
            // 检查输入是否为空或null
            if (string.IsNullOrEmpty(mobileNum))
            {
                return mobileNum;
            }

            // 如果手机号码长度不是11位，直接返回原号码
            if (mobileNum.Length != 11)
            {
                return mobileNum;
            }

            // 使用正则表达式验证是否为纯数字
            if (!Regex.IsMatch(mobileNum, @"^\d{11}$"))
            {
                return mobileNum;
            }

            // 将中间4位数字替换为*
            return mobileNum.Substring(0, 3) + "****" + mobileNum.Substring(7);
        }



    }
}
